#Script now changes current working directory to that of the mathematica package before running package
# Usage is pythonTestDriver.py TestThroughPackage.m
#	where the executable to use is defined in TestThroughPacakge.nb
#		which is saved to TestThroughPackage.m

from time import clock
from subprocess import Popen, PIPE
from sys import argv
import os
t1=clock()
path_to_math = r"c:\Program Files\Wolfram Research\Mathematica\6.0\math.exe"
package_dir, package_fn  = os.path.split(os.path.abspath(argv[1]))
#print "package_dir=", package_dir, "package_fn=", package_fn
os.chdir(package_dir)
print "working directory for package is:", os.getcwd()

p = Popen([path_to_math], stdout=PIPE, stdin = file(package_fn), shell = True) 
out, err = p.communicate()
t2=clock()
print "*"*100 + "\n"
lines = []
for line in out.split("\r\n"):
	try:
		if line.rstrip() == "" or line.rstrip() [-1]=="=":
			continue
		lines.append(line)
	except IndexError, e:
		print "Exception"
		print repr(line)
		print e
print "\n".join(lines)
print file("logtest.txt").read()

if err == None:
	print "no message from stderr"
else:
	print "Message from stderr follows"
	print err

print "Running the package %s took %.2f seconds" %(argv[1], t2-t1)
print "*"*60
print lines[-2]
print lines[-1]
print "*"*60 + "\n"